package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.Material;
import com.ruoyi.system.domain.MaterialFour;
import com.ruoyi.system.domain.Warehouse;
import com.ruoyi.system.service.IWarehouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.util.HashMap;
import java.util.List;

/**
 * 仓库Controller
 * 
 * @author ruoyi
 * @date 2020-12-14
 */
@RestController
@RequestMapping("/system/warehouse")
public class  WarehouseController extends BaseController
{

    @Autowired
    private IWarehouseService warehouseService;

    /**
     * 查询仓库列表
     */
//    @RequiresPermissions("system:warehouse:list")
    @PostMapping("/list")
    public TableDataInfo list(Warehouse warehouse)
    {
        startPage();
        List<Warehouse> list = warehouseService.selectWarehouseList(warehouse);
        return getDataTable(list);
    }
    @PostMapping("/listall")
    public List<Warehouse> listall()
    {
        List<Warehouse> list = warehouseService.quryWarehouseList();
        return list;
    }

    /**
     * 新增保存仓库
     */
    @Log(title = "仓库", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult addSave(@RequestBody Warehouse warehouse)
    {   int a=warehouseService.insertWarehouse(warehouse);
        if(a<=0){
            return error("仓库"+warehouse.getwName()+"已存在");
        }else {
            return toAjax(a);
        }

    }

    /**
     * 修改仓库
     */
    @GetMapping("/edit/{wId}")
    public AjaxResult edit(@PathVariable("wId") Long wid)
    {
        Warehouse warehouse = warehouseService.selectWarehouseById(wid);
        return AjaxResult.success(warehouse);
    }

    /**
     * 修改保存仓库
     */
    @Log(title = "仓库", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult editSave(@RequestBody Warehouse warehouse)
    {
        return toAjax(warehouseService.updateWarehouse(warehouse));
    }

    /**
     * 删除仓库
     */
    @Log(title = "仓库", businessType = BusinessType.DELETE)
    @DeleteMapping( "/remove/{wid}")
    public AjaxResult remove(@PathVariable long wid) {
        if (warehouseService.deleteWarehouseById(wid)> 0){
            return success();
        }else {
            return error("仓库内有材料不能删除");
        }
    }

    /*
     *
     * 仓库明细
     *
     * */
    @PostMapping("/detail/list")
    public TableDataInfo warehouserecordlist(Material material, Long wid)
    {
        startPage();
        HashMap<String , Object> map = new HashMap<>();
        map.put("material",material);
        map.put("wid",wid);
        List<MaterialFour> list = warehouseService.warehouserecordlist(map);
        return getDataTable(list);
    }
    /**
     * 导出仓库列表
     */
    @Log(title = "仓库", businessType = BusinessType.EXPORT)
    @PostMapping("/detail/export")
    public AjaxResult export(Material material, Long wid)
    {
        HashMap<String , Object> map = new HashMap<>();
        map.put("material",material);
        map.put("wid",wid);
        List<MaterialFour> list = warehouseService.warehouserecordlist(map);
        ExcelUtil<MaterialFour> util = new ExcelUtil<MaterialFour>(MaterialFour.class);
        return util.exportExcel(list, "MaterialTwo");
    }

    /**
     * 导出仓库列表
     */
    @Log(title = "仓库", businessType = BusinessType.EXPORT)
    @PostMapping("/detail/upload")
    public void upload(MultipartFile file)
    {
        warehouseService.updateMdType(file);
    }
}
